package com.tyz.hellocst.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.tyz.hellocst.AppContext;
import com.tyz.hellocst.R;
import com.tyz.hellocst.activity.AboutUsActivity;
import com.tyz.hellocst.activity.GoodsListActivity;
import com.tyz.hellocst.activity.LoginActivity;
import com.tyz.hellocst.activity.PersonalEditActivity;

import java.util.ArrayList;

public class PersonalFragment extends Fragment {

    ListView listView;
    Button button,btnAboutUs;
    TextView tvName;
    TextView tvUserName;
    ArrayList arrayList;
    ArrayAdapter arrayAdapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_personal,container,false);

        initID(view);
        initView();


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String str = (String) parent.getItemAtPosition(position);

                Intent intent = new Intent();
                switch (position) {
                    case 0:
                        intent.setClass(getActivity(), PersonalEditActivity.class);
                        startActivity(intent);
                        break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                        intent.setClass(getActivity(), GoodsListActivity.class);
                        intent.putExtra("intentName", str);
                        intent.putExtra("intentPosition", String.valueOf(position));
                        startActivity(intent);
                        break;
                    default:
                        Toast.makeText(getActivity().getApplicationContext(), "点击无效！", Toast.LENGTH_LONG).show();
                        return;
                }
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                getActivity().finish();
            }
        });

        btnAboutUs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AboutUsActivity.class);
                startActivity(intent);
            }
        });

        return view;
    }

    private void initID(View view) {
        listView = (ListView) view.findViewById(R.id.personal_listView);
        button = view.findViewById(R.id.personal_button);
        btnAboutUs = view.findViewById(R.id.button_about_us);
        tvName = (TextView) view.findViewById(R.id.personal_name);
        tvUserName = (TextView) view.findViewById(R.id.personal_userName);
    }

    private void initView() {

        tvUserName.setText(AppContext.getUserName());
        tvName.setText(AppContext.getName());

        arrayList = new ArrayList();
        arrayAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, arrayList);

        arrayList.add(getString(R.string.a_personal_edit));
        arrayList.add(getString(R.string.a_personal_issued));
        arrayList.add(getString(R.string.a_personal_buyed));
        arrayList.add(getString(R.string.a_personal_order));
        arrayList.add(getString(R.string.a_personal_trading));
        arrayList.add(getString(R.string.a_personal_finished));
        arrayList.add(getString(R.string.a_personal_unIssued));
        arrayList.add(getString(R.string.a_personal_unselled));
        listView.setAdapter(arrayAdapter);
    }
}
